﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using DigiLib;

public partial class controls_admin_PartnerEdit : Admin_Controls_Base {
    protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack) {
            base.ModuleByContent(lsbModule, "PARTNER", 1);
            LoadData();
        }
    }
    public void LoadData() {
        try {
            ObjDT = BO_portal_partners.Search(Id);
            txtTITTLE.Text = ObjDT.Rows[0]["PARTNER_NAME"].ToString();
            ctPath.Src = "/" + Setting.Path + ObjDT.Rows[0]["LOGO_PATH"].ToString();
            txtDescription.Text = ObjDT.Rows[0]["DESCRIPTION"].ToString();
            txtEmail.Text = ObjDT.Rows[0]["EMAIL"].ToString();
            txtWebsite.Text = ObjDT.Rows[0]["WEBSITE"].ToString();
            txtOrder.Text = ObjDT.Rows[0]["ORDER_DISPLAY"].ToString();
            ObjDT = BO_cms.Join_Tables_Select(Id, "PORTAL_PARTNERS/PORTAL_MODULES", false);
            foreach (DataRow dr in ObjDT.Rows) {
                foreach (ListItem item in lsbModule.Items) {
                    if (dr["ID_2"].ToString().Trim().Equals(item.Value.Trim()))
                        item.Selected = true;
                    if (Convert.ToInt32(dr["ID_2"]) == Module_ID)
                        txtOrder.Text = dr["VAR1"].ToString();
                }
            }
        } catch (Exception ex) {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e) {
        try {
            ObjDT = BO_portal_partners.Search(Id);
            string title = txtTITTLE.Text.Trim();
            string filename = base.Upload("partner", ObjDT.Rows[0]["LOGO_PATH"].ToString(), fupPath, divMessage, false);
            string description = txtDescription.Text.Trim();
            string email = txtEmail.Text.Trim();
            string website = txtWebsite.Text.Trim();
            int order = Convert.ToInt32(txtOrder.Text.Trim());
            string result = BO_portal_partners.Insert(title, filename, description, email, website, order, Convert.ToDateTime(ObjDT.Rows[0]["CREATED_DATE"]), ObjDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, Administrator.USER_NAME, true);
            if (result == "0") { divMessage.InnerHtml = MessageBox.Error("Hệ thống", "Thực hiện không thành công"); } else {
                divMessage.InnerHtml = MessageBox.Success("Hệ thống", "Thực hiện thành công");
                foreach (ListItem item in lsbModule.Items) {
                    if (item.Selected) {
                        if (Module_ID == Convert.ToInt32(item.Value)) {
                            BO_cms.Join_Tables_Update(Id, Module_ID, "PORTAL_PARTNERS/PORTAL_MODULES", order, 1);
                        } else
                            BO_cms.Join_Tables_Insert(Id, Convert.ToInt32(item.Value), "PORTAL_PARTNERS/PORTAL_MODULES", order, 0);
                    } else {
                        BO_cms.Join_Tables_Delete(Id, Convert.ToInt32(item.Value), "PORTAL_PARTNERS/PORTAL_MODULES");
                    }
                }
                Javascripts.AlertRedirect(this.Page, "Cập nhật thành công!", "default.aspx?control=partner");
            }
        } catch (Exception ex) {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
        }
    }
}
